package com.ywl.mvcframework.util;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.ywl.mvcframework.config.ActionConfig;
import com.ywl.mvcframework.config.FormbeanConfig;

/**
 * mvc框架xml文件解析工具类
 * @author Administrator
 *
 */
public class MvcConfigUtil {

	public static void readConfig(Map<String,ActionConfig> actionConfigMap, 
			Map<String,FormbeanConfig> formbeanConfigMap, String xmlPath){
		SAXReader reader = new SAXReader(); 
		
	    try {
	    	Document  document = reader.read(new File(xmlPath));
			Element root = document.getRootElement();
			
			//解析action-mappings
			Element actionmappingsNode = root.element("action-mappings");
			if(actionmappingsNode != null){
				List<Element> nodes = actionmappingsNode.elements("action");
				for (Element node : nodes) {
					String className = node.attributeValue("className");
					String path = node.attributeValue("path");
					String fmbean = node.attributeValue("fmbean");
					
					ActionConfig ac = new ActionConfig();
					ac.setClassName(className);
					ac.setPath(path);
					ac.setFmbean(fmbean);
					actionConfigMap.put(path, ac);
				}	
			}
			
			
			//解析form-beans
			Element formbeansNode = root.element("form-beans");
			if(formbeansNode != null){
				List<Element> formbeans = formbeansNode.elements("form-bean");
				for (Element formbean : formbeans) {
					String name = formbean.attributeValue("name");
					String type = formbean.attributeValue("type");
					FormbeanConfig fc = new FormbeanConfig();
					fc.setName(name);
					fc.setType(type);
					
					formbeanConfigMap.put(name, fc);
				}
			}
	    } catch (DocumentException e) {
			e.printStackTrace();
		}  
	}
	
}
